package com.sky.controller;

import ch.qos.logback.classic.Logger;
import com.jfinal.aop.Inject;
import com.jfinal.aop.Interceptor;
import com.jfinal.core.NotAction;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.upload.UploadFile;
import com.sky.model.User;
import com.sky.service.UserService;
import io.jboot.web.controller.JbootController;
import io.jboot.web.controller.annotation.RequestMapping;
import org.apache.commons.lang.ObjectUtils;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

/*
 *用户：sky-吴
 *日期：2020/4/22
 */
@RequestMapping("/user")
public class UserController extends JbootController {



    @Inject
    private UserService userService;

    public UserController() {}
    Log logger = Log.getLog(UserController.class);
    public void list() {
        int page = getParaToInt("page", 1);
        //不用同mybatis一样配分页插件了,Page<User> 对象已经包含 分页数据和查询数据
        Page<User> userPage = userService.paginate(page, 10);
        setAttr("pageData", userPage);
        List<User> list = userPage.getList();
        render("/user/user.html");
    }


   public void  index(){

        render("/user/index.html");
   }






    public void toAdd() {

        int id = getParaToInt("id", 0);
        //有id ，说明有数据提交过来，用来做修改的标识。
        if (id > 0) {
            setAttr("id", id);
        }
        render("/user/add.html");

    }

    public void doSave() {
        UploadFile uploadFile = super.getFile();
        //System.out.println(uploadFile.getFileName());
        String loginName = getPara("login_name");
        String password = getPara("password");

        long id = getParaToLong("id", (long) 1);

        //实体类继承了jfinal的model之后, 功能增强了, 每个bean实体都能做单表的crud
        User user = new User();
        user.setLoginName(loginName);
        user.setPassword(password);

        if (id > 0) { //说明是更新
            user.setId((int) id);
            //Db.save("user", user);
            user.update();
        } else { //说明是新增
            user.save();
        }

        redirect("/user/user");
    }

    //http://127.0.0.1:8080/user/getUsers
    public void getUsers() {
        // 这里用到了 userService 的查询方法
        List<User> users = (List<User>) userService.findAll();
        renderText(Arrays.toString(users.toArray()));
    }

    public void del() {
        long id = getParaToLong("id", 0L);
        userService.deleteById(id);
        redirect("/user/user");
    }


    //http://127.0.0.1:8080/user/findByLoginName?loginName=a1
    public void findByLoginName() {
        String loginName = getPara("loginName", "");
        List<Record> records = userService.findByLoginName(loginName);
        //Record record = records.get(0);
        String text = "";
        for (Record record : records) {

            String temp = new String("  id:" + record.getInt("id") +
                    "  login_name:" + record.getStr("login_name") +
                    "  password:" + record.getStr("password")
            );
            text = text + temp;
        }
        renderText(text);
    }

    //http://127.0.0.1:8080/user/getJson
    public void getJson( User user) {

        System.out.println("user.toString() = " + user.toString());
        BufferedReader br;
        StringBuilder sb = new StringBuilder();
        try {
            br = new BufferedReader(new InputStreamReader((ServletInputStream) super.getRequest().getInputStream()));
            String line = null;
            while (((line = br.readLine()) != null)) {
                    sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println("sb.toString() = " + sb.toString());
        renderText(sb.toString());
    }

    @NotAction
    public void getUser() {

        Integer id = super.getParaToInt("id");
        if (id == null) {
            return;
        }
        User user = userService.findById(id);
        super.setAttr("user", user);
    }







    public void getUserByColumns(){

        HashMap<String, String> map =new HashMap<>();
        map.put("username", getPara("username"));
        map.put("password", getPara("password"));
        map.put("filePath", getPara("filePath"));
        map.put("balanceBottom", getPara("balanceBottom"));
        map.put("balanceTop", getPara("balanceTop"));
        map.put("createDateBottom", getPara("createDateBottom"));
        map.put("createDateTop", getPara("createDateTop"));

        List<User> userList = userService.findByColumns(map);
        if ( userList==null ){
            userList =new ArrayList<>();
        }
        renderText(userList.toString());

    }



}
